<?php
/**
 * File description.
 * 
 * @filesource
 * @author Michael N <webjestic@gmail.comt>
 * @link http://webjestic.net webJestic
 * @license http://www.opensource.org/licenses/bsd-license.php BSD
 * @copyright Copyright (c) 2009 - webJestic.NET.  All Rights Reserved.
 * 
 * 
 * @category   Engine
 * @package    Resources
 * @subpackage Database 
 */


class Engine_Resource_Database extends Zend_Application_Resource_ResourceAbstract {
	
	protected $_database;
	protected $_schema;
	
	public function init() {
		$result = $this->getDatabase();
		$this->_schema = new Engine_Db_Schema($this->_database);
		return $result;
	}
	

	public function getDatabase() {
		
		if (null === $this->_database) {
			$options = $this->getOptions();
			try {
				$this->_database = Zend_Db::factory($options['adapter'], $options['params']);
				$this->_database->getConnection();
			} catch (Zend_Db_Adapter_Exception $e) {		
				$msg = $e->getMessage() . '<br/>';
				die ("Unable to connect to database.<br/>$msg");
			} catch (Exception $e) {
				die ("Unknown Exception went uncaught, attempting to connect to the database.");
			}
			
			define('TABLE_PREFIX', $options['params']['tableprefix']);
		}
		
		Zend_Registry::set('Zend_Db', $this->_database);
		Zend_Db_Table_Abstract::setDefaultAdapter($this->_database);
		return $this->_database;		
	}
}

?>